package com.quyeying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.quyeying.entity.TblRegionEntity;
import com.quyeying.mapper.TblRegionDao;
import com.quyeying.service.TblRegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ReturnPageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;

/**
* <p>
 *  服务实现类
 * </p>
*
* @author jll
* @since 2022-07-28
*/
@Service
public class TblRegionServiceImpl extends ServiceImpl<TblRegionDao, TblRegionEntity> implements TblRegionService {

    @Override
    public R findListByPage(Integer page, Integer pageCount){
        IPage<TblRegionEntity> wherePage = new Page<>(page, pageCount);
        TblRegionEntity where = new TblRegionEntity();
        IPage<TblRegionEntity> iPage = baseMapper.selectPage(wherePage, Wrappers.query(where));
        return R.success(ReturnPageData.fillingData(iPage));
    }

    @Override
    public R add(TblRegionEntity tblRegionEntity){
        baseMapper.insert(tblRegionEntity);
        return R.success();
    }

    @Override
    public R delete(Long id){
        baseMapper.deleteById(id);
        return R.success();
    }

    @Override
    public R updateData(TblRegionEntity tblRegionEntity){
        baseMapper.updateById(tblRegionEntity);
        return R.success();
    }

    @Override
    public R findById(Long id){
        TblRegionEntity tblRegionEntity = baseMapper.selectById(id);
        return R.success(tblRegionEntity);
    }

    @Override
    public R findByPareneId(Long id) {
        if (ObjectUtils.isEmpty(id)){
            id = 1L;
        }
        QueryWrapper<TblRegionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID", id);
        List<TblRegionEntity> tblRegionEntityList = baseMapper.selectList(wrapper);
        return R.success(tblRegionEntityList);
    }
}
